package com.elitel.frame.core.util;

import java.io.IOException;
import java.util.Scanner;

public class ComputerNumber {
	
	/**
	 * <pre>GenSN(加密缩短机器码)   
	 * @author：FanWenquan    
	 * @date：2017年10月24日 下午2:30:41   
	 * @param sn
	 * @return</pre>
	 */
	public static String genSN(String sn) {
		//String sn = tm0 + tm1 + computer;
		int len = sn.length();
        if (len < 11)
            return sn;
        char c0 = sn.charAt(0);
        char c1 = sn.charAt(1);
        String result = sn.substring(2);
        c0 = (char)(((int)c0 + (int)c1) / 2);
        while (c0 < 'A')
        {
            int c = c0;
            c += 5;
            c0 = (char)c;
        }
        while (c0 > 'Z')
        {
            int c = c0;
            c -= 6;
            c0 = (char)c;
        }
        result += c0;
		return genSN(result);
	}
	
	/**
	 * <pre>getDiskSerialNumber(获取硬盘序列码)   
	 * @author：FanWenquan    
	 * @date：2017年10月24日 下午2:29:46   
	 * @return</pre>
	 */
	public static String getDiskSerialNumber() {
		  //wmic diskdrive get serialnumber
		  Process process;
		  String serial = "";
		try {
			process = Runtime.getRuntime().exec(
					    new String[] { "wmic", "diskdrive", "get", "serialnumber" });
			process.getOutputStream().close();
			  Scanner sc = new Scanner(process.getInputStream());
			  String property = sc.next();
			  serial = sc.next();
			  //System.out.println(property + "disk: " + serial);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return serial;
	}
	
	/**
	 * <pre>getCpuSerialNumber(获取CPU序列码)   
	 * @author：FanWenquan    
	 * @date：2017年10月24日 下午2:29:55   
	 * @return</pre>
	 */
	public static String getCpuSerialNumber() {
		  long start = System.currentTimeMillis();
		  Process process;
		  String serial = "";
		try {
			process = Runtime.getRuntime().exec(
			    new String[] { "wmic", "cpu", "get", "ProcessorId" });
			process.getOutputStream().close();
			  Scanner sc = new Scanner(process.getInputStream());
			  String property = sc.next();
			  serial = sc.next();
			  //System.out.println(property + "cpu: " + serial);
			  //System.out.println("time:" + (System.currentTimeMillis() - start));
		} catch (IOException e) {
			e.printStackTrace();
		}
		  return serial;
	 }
}
